package com.example.cmpe277stockdata;

import java.util.ArrayList;

import com.example.cmpe277stockdata.RSSFeeds.CompanyInformationReader;
import com.example.cmpe277stockdata.beans.CompanyInformationBean;
import com.example.cmpe277stockdata.beans.NewsBean;
import com.example.cmpe277stockdata.beans.UserBean;
import com.example.cmpe277stockdata.constants.DataConstants;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class CompanyInformationActivity extends Activity {

	String companySymbol = "",companyname="";
	private ArrayList<NewsBean> newsList = null;
	CompanyInformationReader reader = new CompanyInformationReader();
	private UserBean user;
	private TextView companyName, dailyVolume, percentageChange, shareDivident, daysLow, daysHigh, yearsLow, yearsHigh, MarketCapitalization,
					 peRatio, Sector, Industry, EmployeeCount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_company_information);
		newsList = (ArrayList<NewsBean>) getIntent().getSerializableExtra(DataConstants.NEWS_RESULT);
		companyname = getIntent().getStringExtra(DataConstants.COMPANY_NAME);
		companySymbol = getIntent().getStringExtra(DataConstants.TICKER_SYMBOL);
		user = (UserBean) getIntent().getSerializableExtra(DataConstants.USER_DATA);
		getReferenceToAllTextFields();
		CompanyInformationData companyInfo = new CompanyInformationData();
		companyInfo.execute();
		
	}

	private void getReferenceToAllTextFields() {
		companyName = (TextView) findViewById(R.id.companyname);
		dailyVolume = (TextView) findViewById(R.id.companymarketvolume);
		percentageChange = (TextView) findViewById(R.id.companysPercentageChange);
		shareDivident = (TextView) findViewById(R.id.companysShareDivident);
		daysLow = (TextView) findViewById(R.id.companydayslow);
		daysHigh = (TextView) findViewById(R.id.companydayshigh);
		yearsHigh = (TextView) findViewById(R.id.companyyearshigh);
		yearsLow = (TextView) findViewById(R.id.companyyearslow);
		MarketCapitalization = (TextView) findViewById(R.id.companysMarketCapitalization);
		peRatio = (TextView) findViewById(R.id.companysPE);
		Sector = (TextView) findViewById(R.id.companysSector);
		Industry = (TextView) findViewById(R.id.companysIndustry);
		EmployeeCount = (TextView) findViewById(R.id.companysEmployees);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.company_information, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
	    case R.id.action_home:
	    	if(user != null)
	    	{
	    		Intent intent = new Intent(this, InitialTabContent.class);
				intent.putExtra(DataConstants.USER_DATA, user);
		        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        startActivity(intent);
	    	}
	    	else
	    	{
	    		Intent intent = new Intent(this, LandingActivity.class);
				intent.putExtra(DataConstants.USER_DATA, user);
		        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        startActivity(intent);
	    	}
	        return true;
	    case R.id.action_historical:
	        Intent intent1 = new Intent(this, HistoricalActivity.class);
	        intent1.putExtra(DataConstants.NEWS_RESULT, newsList);
        	intent1.putExtra(DataConstants.COMPANY_NAME, companyname);
			intent1.putExtra(DataConstants.USER_DATA, user);
        	intent1.putExtra(DataConstants.TICKER_SYMBOL,companySymbol);
	        intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(intent1);
	        return true;
	    case R.id.action_companyNews:
	        Intent intent2 = new Intent(this, NewsActivity.class);
	        intent2.putExtra(DataConstants.NEWS_RESULT, newsList);
        	intent2.putExtra(DataConstants.COMPANY_NAME, companyname);
        	intent2.putExtra(DataConstants.TICKER_SYMBOL,companySymbol);
			intent2.putExtra(DataConstants.USER_DATA, user);
	        intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(intent2);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	public class CompanyInformationData extends
			AsyncTask<String, Void, CompanyInformationBean> {

		@Override
		protected void onPostExecute(CompanyInformationBean result) {
			try {
				if (result != null) {
					companyName.setText(companyName.getText() + "  :  " + result.getCompanyFullName());
					dailyVolume.setText(dailyVolume.getText() + "  :  " + result.getAverageDailyVolume());
					percentageChange.setText(percentageChange.getText() + "  :  " + result.getPercentChange());
					shareDivident.setText(shareDivident.getText() + "  :  " + result.getDividendShare());
					daysLow.setText(daysLow.getText() + "  :  " + result.getDaysLow());
					daysHigh.setText(daysHigh.getText() + "  :  " + result.getDaysHigh());
					yearsHigh.setText(yearsHigh.getText() + "  :  " + result.getYearsHigh());
					yearsLow.setText(yearsLow.getText() + "  :  " + result.getYearsLow());
					MarketCapitalization.setText(MarketCapitalization.getText() + "  :  " + result.getMarketCapitalization());
					peRatio.setText(peRatio.getText() + "  :  " + result.getPERatio());
					Sector.setText(Sector.getText() + "  :  " + result.getSector());
					Industry.setText(Industry.getText() + "  :  " + result.getIndustry());
					EmployeeCount.setText(EmployeeCount.getText() + "  :  " + result.getFullTimeEmployees());
					} else {
						
					}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		protected CompanyInformationBean doInBackground(String... params) {
			CompanyInformationBean result = null;
			//companySymbol = "CSCO";
			if (companySymbol != null || !companySymbol.equalsIgnoreCase(""))
			{
				result = reader.getCompanyInformation(companySymbol);// companyName
				return result;
			}
			else
				return result;
		}

	}
}
